package com.componentscan.processor;

import com.componentscan.acomponent.Foo;
import com.componentscan.anno.Funny;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

/**
 * @author fanxiao  2022/4/26
 * @since 1.0.0
 */
@Slf4j
@Component
public class FooBeanPostProcessor implements BeanPostProcessor, EnvironmentAware {

	private Environment environment;

	@Override
	public void setEnvironment(Environment environment) {
		this.environment = environment;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if (beanName.equalsIgnoreCase("foo")) {
			Foo foo = (Foo) bean;
			Funny funny = AnnotationUtils.findAnnotation(foo.getClass(), Funny.class);
			if (funny != null) {
				String config = funny.value();
				String result = environment.resolvePlaceholders(config);
				log.info("config:{}", result);
			}

		}
		return bean;
	}
}